package com.cai.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.cai.constant.MessageConstant;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.entity.Result;
import com.cai.pojo.CheckGroup;
import com.cai.pojo.Permission;
import com.cai.pojo.Role;
import com.cai.service.PermissionService;
import com.cai.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.add(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
        return new Result(true, "添加成功");
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }



    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;

    }
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = roleService.findAll();
            return new Result(true,"查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
          Role role = roleService.findById(id);
            return new Result(true, "查询成功", role);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    //根据角色id查询对应的权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "查询成功", list);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //编辑修改角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.edit(role, permissionIds);
            return new Result(true,"编辑成功");//编辑成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }
}
